<?php namespace App\Http\Controllers;

use App\Answers;
use App\Banner;
use App\Classify;
use App\DakaContent;
use App\Page;
use App\Plan;
use App\PlanJoin;
use App\Profession;
use App\Questions;
use App\Content;
use App\SysFeedback;
use App\User;
use App\UserInfo;
use App\VisitLog;
use Illuminate\Contracts\Auth\Guard;
use Auth;
use Illuminate\Http\Request;

class AdminController extends Controller
{
    public function login(){
        return view('admin.login');
    }

    public function questionList(){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $allQuestions = Questions::orderBy('id','desc')->get();
        if(count($allQuestions) > 0){
            $i = count($allQuestions);
            foreach ($allQuestions as $question){
                $allAnswers = Answers::whereQid($question->id)->orderBy('select_num','asc')->get();
                $question->answers = $allAnswers;
                $question->num = $i;
                $i--;
            }
        }
        return view('admin.question-list',['questions'=>$allQuestions]);
    }

    public function questionAdd(Request $request){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $id = $request->get('id');

        /** @var Questions $thatQuestion */
        $thatQuestion = Questions::whereId($id)->first();

        return view('admin.question-add',["question"=>$thatQuestion]);
    }

    public function answerList(Request $request){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $qid = $request->get('qid');

        $thatQuestion = Questions::whereId($qid)->first();

        $allAnswer = Answers::whereQid($qid)->get();

        return view("admin.answer-list",["answers"=>$allAnswer]);
    }

    public function answerSet(Request $request){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $aid = $request->get('aid');

        $theAnswer = Answers::whereQid($aid)->get();

        return view("admin.answer-set",["answer"=>$theAnswer]);
    }

    public function dakaContentList(){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $allDakaContent = DakaContent::orderBy('id','desc')->get();

        return view('admin.daka-content-list',["dakaList"=>$allDakaContent]);
    }

    public function dakaContentSet(Request $request){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $did = !is_null($request->get('d_id')) ? $request->get('d_id') : 0;

        /** @var DakaContent $thatDaka */
        $thatDaka = DakaContent::whereId($did)->first();


        return view('admin.daka-content-set',["dakaInfo"=>$thatDaka]);
    }

    public function userList(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $users = User::orderBy("created_at","desc")->paginate(50);

        foreach($users as $user){
            /** @var User $user */
            $user->userInfo;
        }

        return view('admin.user-list',["users"=>$users]);
    }

    public function bannerList(){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $sysBanners = Banner::orderBy('sort','asc')->orderBy('created_at','desc')->get();

        if(count($sysBanners) > 0){
            foreach ($sysBanners as $sysBanner){
                $thatClassify = Classify::whereId($sysBanner->classify_id)->first();
                $sysBanner->classify =$thatClassify?$thatClassify->name : "-";
            }
        }

        return view('admin.banner-list',['banners'=>$sysBanners]);
    }

    public function bannerSet(Request $request){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $id = !is_null($request->get('id'))?$request->get('id'):0;

        $thatBanner = Banner::whereId($id)->first();

        $classifys = Classify::all();

        return view('admin.banner-set',['banner'=>$thatBanner,"classifys"=>$classifys]);
    }

    public function feedbackList(){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $allFeedback = SysFeedback::orderBy('created_at','desc')->get();

        return view('admin.feedback-list',[
            'feedbacks' => $allFeedback
        ]);
    }

    public function contentsList(Request $request){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $contents = Content::orderBy('created_at','desc')->get();

        return view('admin.contents-list',['contents' => $contents]);
    }

    public function contentsSet(Request $request){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $id = !is_null($request->get('id'))?$request->get('id'):0;


        $sysContents = Content::whereId($id)->first();

        $classifys = Classify::all();

        return view('admin.contents-set',['sysContents' => $sysContents,"classifys"=>$classifys]);
    }

    public function changePwd(){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        return view('admin.change-psw');
    }
}